<template>
  <div class="system-settings">
    <el-tabs tab-position="left">
      <el-tab-pane label="基本设置">
        <el-form
          ref="settingsFormRef"
          :model="settingsForm"
          :rules="settingsRules"
          label-width="120px"
        >
          <el-form-item label="公司名称" prop="companyName">
            <el-input v-model="settingsForm.companyName" />
          </el-form-item>
          <el-form-item label="联系电话" prop="phone">
            <el-input v-model="settingsForm.phone" />
          </el-form-item>
          <el-form-item label="联系地址" prop="address">
            <el-input v-model="settingsForm.address" />
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="settingsForm.email" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSaveSettings">保存</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="库存预警">
        <el-table :data="inventoryWarnings" border stripe>
          <el-table-column prop="productName" label="商品名称" />
          <el-table-column prop="productCode" label="商品编码" />
          <el-table-column prop="inventoryQuantity" label="当前库存" />
          <el-table-column prop="warningQuantity" label="预警数量" />
          <el-table-column label="操作" width="120">
            <template #default="{ row }">
              <el-button link type="primary" @click="openEditDialog(row)">编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="数据字典">
        <div class="dict-header">
          <el-button type="primary" @click="handleAddRootDict">新增顶级字典</el-button>
        </div>
        <el-tree
          :data="dictTree"
          :props="dictProps"
          :expand-on-click-node="false"
          default-expand-all
        >
          <template #default="{ node, data }">
            <span class="custom-tree-node">
              <span>{{ node.label }}</span>
              <span>
                <el-button link type="primary" @click="openEditDictDialog(data)">
                  编辑
                </el-button>
                <el-button link type="primary" @click="handleAddChildDict(data)">
                  新增
                </el-button>
                <el-button
                  link
                  type="danger"
                  :disabled="data.children && data.children.length > 0"
                  @click="handleDeleteDict(node, data)"
                >
                  删除
                </el-button>
              </span>
            </span>
          </template>
        </el-tree>
      </el-tab-pane>
    </el-tabs>

    <el-dialog v-model="editDialogVisible" title="编辑预警设置" width="400px">
      <el-form :model="currentWarning" label-width="100px">
        <el-form-item label="预警数量">
          <el-input-number v-model="currentWarning.warningQuantity" :min="0" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSaveWarning">确定</el-button>
      </template>
    </el-dialog>

    <el-dialog
      v-model="dictDialogVisible"
      :title="dictDialogTitle"
      width="400px"
      @closed="resetDictForm"
    >
      <el-form
        ref="dictFormRef"
        :model="dictForm"
        :rules="dictRules"
        label-width="100px"
      >
        <el-form-item label="字典名称" prop="dictName">
          <el-input v-model="dictForm.dictName" />
        </el-form-item>
        <el-form-item label="字典编码" prop="dictCode">
          <el-input v-model="dictForm.dictCode" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dictDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSaveDict">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

const settingsForm = reactive({
  companyName: '',
  phone: '',
  address: '',
  email: ''
})

const settingsRules = {
  companyName: [{ required: true, message: '请输入公司名称', trigger: 'blur' }],
  phone: [
    { required: true, message: '请输入联系电话', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  address: [{ required: true, message: '请输入联系地址', trigger: 'blur' }],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ]
}

const settingsFormRef = ref(null)

const handleSaveSettings = () => {
  settingsFormRef.value.validate(valid => {
    if (valid) {
      // 调用保存系统设置的API
      ElMessage.success('系统设置保存成功')
    }
  })
}

const inventoryWarnings = ref([])

const editDialogVisible = ref(false)
const currentWarning = ref(null)

const openEditDialog = row => {
  currentWarning.value = { ...row }
  editDialogVisible.value = true
}

const handleSaveWarning = () => {
  // 调用保存库存预警设置的API
  ElMessage.success('库存预警设置保存成功')
  editDialogVisible.value = false
}

const dictTree = ref([])
const dictProps = {
  label: 'dictName',
  children: 'children'
}

const dictDialogVisible = ref(false)
const dictDialogTitle = ref('')
const dictForm = ref({})
const dictFormRef = ref(null)

const dictRules = {
  dictName: [{ required: true, message: '请输入字典名称', trigger: 'blur' }],
  dictCode: [{ required: true, message: '请输入字典编码', trigger: 'blur' }]
}

const resetDictForm = () => {
  dictForm.value = {
    id: undefined,
    dictName: '',
    dictCode: '',
    parentId: undefined
  }
}

const handleAddRootDict = () => {
  dictDialogTitle.value = '新增顶级字典'
  dictDialogVisible.value = true
}

const handleAddChildDict = data => {
  dictDialogTitle.value = '新增子字典'
  dictForm.value.parentId = data.id
  dictDialogVisible.value = true
}

const openEditDictDialog = data => {
  dictDialogTitle.value = '编辑字典'
  dictForm.value = { ...data }
  dictDialogVisible.value = true
}

const handleSaveDict = () => {
  dictFormRef.value.validate(valid => {
    if (valid) {
      if (dictForm.value.id) {
        // 更新字典
        const index = dictTree.value.findIndex(item => item.id === dictForm.value.id)
        if (index !== -1) {
          dictTree.value.splice(index, 1, dictForm.value)
        }
      } else {
        // 新增字典
        dictForm.value.id = Date.now()
        if (dictForm.value.parentId) {
          const parent = dictTree.value.find(item => item.id === dictForm.value.parentId)
          if (parent) {
            parent.children = parent.children || []
            parent.children.push(dictForm.value)
          }
        } else {
          dictTree.value.push(dictForm.value)
        }
      }
      dictDialogVisible.value = false
      ElMessage.success('字典保存成功')
    }
  })
}

const handleDeleteDict = (node, data) => {
  ElMessageBox.confirm('确定删除该字典吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    const parent = node.parent
    const children = parent.data.children || parent.data
    const index = children.findIndex(d => d.id === data.id)
    children.splice(index, 1)
    ElMessage.success('删除成功')
  })
}
</script>

<style scoped>
.system-settings {
  padding: 20px;
}

.dict-header {
  margin-bottom: 20px;
}

.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 24px;
}
</style> 